import mx.xpath.XPathAPI;

import org.molamil.acto.AppParseConfVO;
import org.molamil.acto.core.Logger;
import org.molamil.acto.core.ParserError;
import org.molamil.acto.props.PropsParser;
import org.molamil.commons.StringUtil;

/**
 * XXX: Comment.
 * 
 * @author Jorge Hernandez
 */
class org.molamil.acto.AppParser {
	
	
	// Constants
	
	public static var ERROR_INVALID_ROOT:String = "ERROR: Invalid root node: <acto> expected, <{node}> found.";
	
	
	// Properties
	
	private static var log:Logger = Logger.getLogger("AppParser");
	
	
	// Functions
	
	public static function parse(xml:XML):AppParseConfVO {
		
		log.info("parse()");
		
		// Check the root node.
		if (xml.firstChild.nodeName != "acto") {
			log.severe("ParserError");
			var msg:String = StringUtil.replace(ERROR_INVALID_ROOT, "{node}", xml.firstChild.nodeName);
			throw new ParserError(msg);
		}
		
		// Create the VO.
		var vo:AppParseConfVO = new AppParseConfVO();
		
		// Get pages.
		var pagesNode:XMLNode = XPathAPI.selectSingleNode(xml.firstChild, "/acto/pages");
		if (pagesNode != null)
			vo.pagesXml = new XML(pagesNode.toString());
		
		// Get commands.
		var commandsNode:XMLNode = XPathAPI.selectSingleNode(xml.firstChild, "/acto/commands");
		if (commandsNode != null)
			vo.commandsXml = new XML(commandsNode.toString());
		
		// Get props.
		vo.props = PropsParser.parse(new XML(xml.firstChild.toString()));

		return vo;
		
	}
	
	
}